/*
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef PANDA_IRTOC_INTERPRETER_UTILS_H_
#define PANDA_IRTOC_INTERPRETER_UTILS_H_

// NOLINTBEGIN(readability-identifier-naming)

namespace panda::interpreter {

% # If it is GN Build with disabled asmjit and irtoc is disabled
#if defined(PANDA_TARGET_AMD64) && !defined(PANDA_COMPILER_TARGET_X86_64)
% Panda::instructions.each do |i|
    static constexpr void (*HANDLE_FAST_<%= i.handler_name %>)() = nullptr;
% end

% Panda::instructions.each do |i|
    static constexpr void (*HANDLE_FAST_<%= i.handler_name %>_LLVM)() = nullptr;
% end

% Panda::prefixes.each do |p|
    static constexpr void (*HANDLE_FAST_<%= p.handler_name %>)() = nullptr;
% end

% Panda::prefixes.each do |p|
    static constexpr void (*HANDLE_FAST_<%= p.handler_name %>_LLVM)() = nullptr;
% end

    static constexpr void (*HANDLE_FAST_INVALID)() = nullptr;
    static constexpr void (*HANDLE_FAST_EXCEPTION)() = nullptr;

    static constexpr void (*HANDLE_FAST_INVALID_LLVM)() = nullptr;
    static constexpr void (*HANDLE_FAST_EXCEPTION_LLVM)() = nullptr;

#else
% Panda::instructions.each do |i|
    extern "C" void HANDLE_FAST_<%= i.handler_name %>();
% end
% Panda::prefixes.each do |p|
    extern "C" void HANDLE_FAST_<%= p.handler_name %>();
% end

% Panda::instructions.each do |i|
    extern "C" void HANDLE_FAST_<%= i.handler_name %>_LLVM();
% end
% Panda::prefixes.each do |p|
    extern "C" void HANDLE_FAST_<%= p.handler_name %>_LLVM();
% end
    extern "C" void HANDLE_FAST_INVALID();
    extern "C" void HANDLE_FAST_EXCEPTION();

    extern "C" void HANDLE_FAST_INVALID_LLVM();
    extern "C" void HANDLE_FAST_EXCEPTION_LLVM();
#endif

// NOLINTNEXTLINE(readability-function-size)
inline void* SetupDispatchTableImpl()
{
    static const std::array<void (*)(), <%= Panda::dispatch_table.handler_names.size() + 1 %>> dispatch_table {
#if defined(PANDA_TARGET_AMD64) && !defined(PANDA_COMPILER_TARGET_X86_64)
% Panda::dispatch_table.handler_names.each do |name|
        HANDLE_FAST_<%= name %>,
% end
#else
% Panda::dispatch_table.handler_names.each do |name|
        &HANDLE_FAST_<%= name %>,
% end
        &HANDLE_FAST_EXCEPTION
#endif
    };
    return (void *)dispatch_table.data();
}

// NOLINTNEXTLINE(readability-function-size)
inline void* SetupLLVMDispatchTableImpl()
{
    static const std::array<void (*)(), <%= Panda::dispatch_table.handler_names.size() + 1 %>> dispatch_table {
#if defined(PANDA_TARGET_AMD64) && !defined(PANDA_COMPILER_TARGET_X86_64)
% Panda::dispatch_table.handler_names.each do |name|
        HANDLE_FAST_<%= name %>_LLVM,
% end
#else
% Panda::dispatch_table.handler_names.each do |name|
        &HANDLE_FAST_<%= name %>_LLVM,
% end
        &HANDLE_FAST_EXCEPTION_LLVM
#endif
    };
    return (void *)dispatch_table.data();
}

}  // namespace panda::interpreter

// NOLINTEND(readability-identifier-naming)

#endif  // PANDA_IRTOC_INTERPRETER_UTILS_H_
